package com.admin.service;

import com.admin.bean.FindInfor;
import com.admin.dbdao.FindAdminDao;
import com.data.util.CheckDataUtils;
import com.data.util.DatabaseIdUtils;
import com.data.util.FileResumeUtils;
import com.data.util.FinalStringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 发现管理的服务层
 *
 * @author:HingLo
 * @create 2017-10-19 10:13
 **/
@Service
public class FindAdminService {
    private static Logger logger = Logger.getLogger(FindAdminService.class);
    @Autowired
    private FindAdminDao findAdminDao;


    public String imageUploadService(CommonsMultipartFile file) {
        //保存文件到指定的文件路径下
        String filename = file.getOriginalFilename();
        //获取文件的后缀
        String s = filename.substring(filename.lastIndexOf("."));

        if (CheckDataUtils.iconUtils(s)) {
            return FileResumeUtils.saveFindImageUtils(file, s, 0);

        } else if (CheckDataUtils.videoUtils(s)) {
            return FileResumeUtils.saveFindImageUtils(file, s, 1);
        }
        return null;


    }


    /**
     * 保存发现的信息
     *
     * @param findInfor
     * @param file
     * @return
     */
    public boolean submitFindContentService(FindInfor findInfor, CommonsMultipartFile file) {
        if (file != null && findInfor != null) {
            findInfor.setCtime(new Date());
            findInfor.setId(DatabaseIdUtils.getDataIdOne());
            findInfor.setImage(file.getBytes());
            findInfor.setState(FinalStringUtils.STATE_DEFAULT);
            //新版本中的链
            findInfor.setType(true);
            return findAdminDao.saveMassageDao(findInfor);
        }
        return false;

    }

    public boolean startFindService(String id, boolean state) {

        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            return findAdminDao.updateFindDao(id, state);
        }

        return false;
    }

    public boolean deleteFindService(String id) {

        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            return findAdminDao.deleteFindDao(id);
        }

        return false;
    }

    public List<FindInfor> inputFindlistService() {
        return this.findAdminDao.inputFindlistDao();
    }


}
